package com.management.controller;

import com.management.po.User;
import com.management.service.UserService;
import com.management.tools.codes;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Calendar;
import java.util.Date;

/**
 * 用户控制器
 */
@Controller
public class UserController {
    Logger logger = Logger.getLogger(UserController.class);
    @Resource
    UserService userService;
    @RequestMapping("/tologin")
    public String tologin(){
        logger.info("进入去登录控制器");
        return "login";
    }
    @RequestMapping("/login")
    public String login(User user, String captcha, Model model, HttpSession session){
        logger.info("验证码："+captcha);
        String currentcaptcha =session.getAttribute("simpleCaptcha").toString();
        if (captcha.equals(currentcaptcha)){
            User currentUser = userService.findByTelAndPass(user.getUserTel(),user.getUserPassword());
            logger.info(user.getUserPassword());
            logger.info(user.getUserTel());
            logger.info(currentUser);
            logger.info(currentUser.getUserId());
            if (currentUser!=null){
                session.setAttribute("currentUser",currentUser);
                System.out.println("进入登录后页面控制器");
                return "redirect:http://localhost:8080/management/userandcompany/userandcompanylist.action?userId="+currentUser.getUserId();
            }else{
                model.addAttribute("message","用户账号或密码错误");
                return "login";
            }
        }else{
            model.addAttribute("message","用户验证码输入错误");
            return "login";
        }
    }
    @RequestMapping("/toadd")
    public String toadd(){
        logger.info("进入了去添加控制器");
        return "userregister";
    }
    @RequestMapping("/adduser")
    public String add(User user){
        logger.info("进入了注册控制器");
        logger.info(user.getUserIdNumber());
        //从身份中信息中截取生日信息
        int year = Integer.valueOf(user.getUserIdNumber().substring(6,10));
        logger.info(year);
        int month=Integer.valueOf(user.getUserIdNumber().substring(10, 12))-1;
        logger.info(month);
        int day=Integer.valueOf(user.getUserIdNumber().substring(12, 14));
        logger.info(day);
        int sexNum=Integer.valueOf(user.getUserIdNumber().substring(16, 17));
        logger.info(sexNum);

        //根据身份证获得性别
        char userSex=' ';
        if(sexNum%2==0){
            userSex='女';
        }else{
            userSex='男';
        }
        //获得当前时间
        Calendar date=Calendar.getInstance();
        //获得年龄
        int userAge = date.get(Calendar.YEAR)-year;
        //根据身份证获得生日
        date.set(year, month, day);
        Date userBirthday=date.getTime();
        user.setUserSex(userSex);
        user.setUserAge(userAge);
        user.setUserBirthday(userBirthday);
        String pro = codes.getDomicile(user.getUserIdNumber());
        logger.info(pro);
        user.setUserProvince(codes.getDomicile(user.getUserIdNumber()));
        boolean flag = userService.add(user);
        logger.info(flag);
        return "login";
    }
    @RequestMapping("/toahthenticateduser")
    public String toahthenticateduser(){
        logger.info("进入======控制器");
        return "usertest";
    }
     @RequestMapping("/ahthenticateduser")
    public String ahthenticateduser(User user ,HttpServletRequest request){
        logger.info("进入验证控制器");
        String userTel = user.getUserTel();
        logger.info(userTel);
        String userName = user.getUserName();
        logger.info(userName);
        String userIdNumber = user.getUserIdNumber();
        logger.info(userIdNumber);
       boolean flag =  userService.usertest(userTel,userName,userIdNumber);
       logger.info(flag);
       if (flag){
           User u= userService.findByTel(userTel);
           int userId= u.getUserId();
           request.setAttribute("userId",userId);
           return "usereditpass";

       }else{
          request.setAttribute("mes","您输入的信息有误，请重新输入！！！");
       }
       return "usertest";
     }
     @RequestMapping("/usereditpass")
    public String usereditpass(int userId,String ctuserPassword){
        logger.info("进入====修改密码控制器");
        User user = userService.findByUserId(userId);
        logger.info(user);
        user.setUserPassword(ctuserPassword);
        userService.update(user);
        return "login";
     }
    //去到修改个人信息页面控制器
     @RequestMapping("/toupdateuser")
    public String toUpdateUser(){
        return "updateuser";
     }

     @RequestMapping("/updateuser")
     public String updateUser(HttpSession session){
        logger.info("525615");
        Object obj=session.getAttribute("currentUser");
         User user=null;
        if(obj!=null){
            user = (User)obj;
        }
         return "redirect:http://localhost:8080/management/userandcompany/userandcompanylist.action?userId="+user.getUserId();
     }
     //退出登录
     @RequestMapping("exit")
    public String exit(HttpSession session){
        session.invalidate();
        return "redirect:http://localhost:8080/management/index.action";
     }
}